<?php
class UsersController extends AppController {
	var $uses = array('User');
    public function beforeFilter() {
        parent::beforeFilter();
		//$this->Auth->allow('add');
    }
	
	public function index(){
		$rs = $this->User->find('all');
		$this->set(compact('rs'));
	}

    public function login() {
		if ($this->request->is('post')) {
			if ($this->Auth->login()) {
				$this->Session->setFlash(__('Ðang nh?p thành công'));
				$this->redirect($this->Auth->redirect());
			} else {
				$this->Session->setFlash(__('Sai tên dang nh?p ho?c m?t kh?u, xin vui lòng th? l?i'));
			}
		}
	}

	public function logout() {
		$this->redirect($this->Auth->logout());
	}
	
	public function view($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Không tìm th?y ngu?i dùng'));
        }
        $this->set('user', $this->User->read(null, $id));
    }

    public function add() {
        if ($this->request->is('post')) {
            $this->User->create();
			if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Tài kho?n dã du?c luu'));
                $this->redirect(array('action' => '.'));
            } else {
                $this->Session->setFlash(__('Có l?i, không luu du?c, xin th? l?i'));
            }
        }
    }

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Không tìm th?y tài kho?n'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('Tài kho?n dã du?c luu'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('Có l?i, không luu du?c, xin th? l?i'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
    }

    public function delete($id = null) {        
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Không tìm th?y tài kho?n'));
        }
        if ($this->User->delete()) {
            $this->Session->setFlash(__('Tài kho?n dã du?c xóa'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Có l?i, không xóa du?c tài kho?n'));
        $this->redirect(array('action' => 'index'));
    }
}
?>